//
//  MSHeroSprite.m
//  MagicSword
//
//  Created by Netease on 15/11/20.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSHeroSprite.h"

#define SizeAddTemp 5
@implementation MSHeroSprite {
    SKTextureAtlas     *_atlas;
    MSHeroInfo         *_heroInfo;
    SKLabelNode        *_finishLabel;
}

- (id)initWithHeroInfo:(MSHeroInfo *)info withSize:(CGSize)size {
    size = CGSizeMake(size.width+SizeAddTemp, size.width+SizeAddTemp);
    self = [super initWithColor:[UIColor clearColor] size:size];
    if (self) {
        _atlas = [SKTextureAtlas atlasNamed:@"hero_thrumb"];
        
        _heroInfo = info;
        SKTexture *texture = [_atlas textureNamed:info.mapImage];
        [self setTexture:texture];
        
        [self setHeroStatus:MSHeroSpriteStatus_Normal];
        
        _finishLabel = [[SKLabelNode alloc] initWithFontNamed:@"Helvetica"];
        _finishLabel.fontColor = [UIColor whiteColor];
        _finishLabel.fontSize = 13;
        _finishLabel.zPosition = 2;
        _finishLabel.hidden = YES;
        _finishLabel.position = CGPointMake(size.width/2-12, -size.height/2+10);
        _finishLabel.text = @"E";
        [self addChild:_finishLabel];
    }
    
    return self;
}

- (MSHeroInfo *)heroInfo {
    return _heroInfo;
}

- (void)setHeroStatus:(MSHeroSpriteStatus)status {
    _heroStatus = status;
    NSMutableArray *textures = [[NSMutableArray alloc] init];
    SKAction *imageAction = nil;
    _finishLabel.hidden = YES;
    
    [self removeAllActions];
    switch (_heroStatus) {
        case MSHeroSpriteStatus_Normal:
        case MSHeroSpriteStatus_Runed:
            for (int i=1; i<=3; i++) {
                SKTexture *texture = [_atlas textureNamed:[NSString stringWithFormat:@"%@_Normal%d", _heroInfo.mapImage, i]];
                [textures addObject:texture];
            }
            imageAction = [SKAction animateWithTextures:textures timePerFrame:0.3];
            [self runAction:[SKAction repeatAction:imageAction count:INT16_MAX]];
            break;
            
        case MSHeroSpriteStatus_Selected:
            for (int i=1; i<=3; i++) {
                SKTexture *texture = [_atlas textureNamed:[NSString stringWithFormat:@"%@_Selected%d", _heroInfo.mapImage, i]];
                [textures addObject:texture];
            }
            imageAction = [SKAction animateWithTextures:textures timePerFrame:0.3];
            [self runAction:[SKAction repeatAction:imageAction count:INT16_MAX]];
            break;
            
        case MSHeroSpriteStatus_Finish:
            _finishLabel.hidden = NO;
            // 查看是否获得物品
            [[MSGameManager shareManager] getItemWithHero:_heroInfo point:self.position];
            break;
            
        default:
            break;
    }
}



@end
